package cn.itcast.controller;

import cn.itcast.entity.MessageConstant;
import cn.itcast.entity.RedisMessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.vo.OrderVO;
import cn.itcast.service.OrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;


@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    JedisPool jedisPool;

    @Reference
    OrderService service;

    @RequestMapping("/findById.do")
    public OrderVO findById(int id) {

        return service.findById(id);

    }



    @RequestMapping("/submit.do")
    public Result submit(@RequestBody Map map) throws Exception {

        //读取redis中存放的手机验证码
        String telephone = map.get("telephone").toString();
        String key = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);

        //从页面获取客户填写的验证码
        String validateCode = map.get("validateCode").toString();

        //验证码对比
        if(validateCode.equals(key)){

            map.put("orderType", Order.ORDERTYPE_WEIXIN);

            //进行预约操作
            Result result = service.order(map);

            //预约成功
            if(result.isFlag()){
                //发送成功短信
                //SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,map.get("orderDate").toString());
                System.out.println("您已成功预约");
            }

            return result;

        }else{
            //验证码对比失败返回此信息
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);

        }
    }
}
